/**
 * Created by isoft-work 5 on 12/14/2014.
 */
$(document).ready(function(){
//   render
   _product = null;
   _productDetailSelect = null;
   _productDetailImage = null;
   renderProductDetail();
});

function renderProductDetail() {
   getProductDetail();
}

function getProductDetail() {
   var _maSP = ISS.SESSION.getSession('session_maSP');

   ISS.BACKEND.request({
      url: ISS.SESSION.getSession('contextPath') + '/service/product/getProductDetail/' + _maSP,
      type: 'GET',
      success: function (data) {
         var responseInfo = data.responseObject;
         _product = responseInfo;
         getProductDetailComplete(responseInfo);
         getProductFromRule(_maSP);
      }
   });
}

function getProductFromRule(maSP) {
   //ISS.CONTROL.lockScreen('#recommentClothes');
   ISS.BACKEND.request({
      url: ISS.SESSION.getSession('contextPath') + '/service/product/getProductFromRule/' + maSP,
      type: 'GET',
      success: function (data) {
         var responseInfo = data.responseObject;
         if(responseInfo) {
            mapProductToView(responseInfo);
         }
         //ISS.CONTROL.unLockScreen('#recommentClothes');
      }
   });
}

function mapProductToView(lstProduct) {
   var product = '';
   for(var i = 0; i < lstProduct.length; i++) {
      var size = '', cost, image;
      var _chiTietSanPhams = lstProduct[i].chiTietSanPhams,
         _listSize = lstProduct[i].size;
      for(var s = 0; s < _listSize.length; s++) {
         size += ISS.STRING_UTIL.format(TEMPLATE.Product.itemDetailSize,{
            tenSize: _listSize[s].tenSize
         })
      }
      for(var c = 0; c < _chiTietSanPhams.length; c++) {
         if(_chiTietSanPhams[c].chiTietKhuyenMai.length > 0) {
            // check date sale off
            var _ngayBD = _chiTietSanPhams[c].chiTietKhuyenMai[0].khuyenMai.ngayBD;
            var _ngayKT = _chiTietSanPhams[c].chiTietKhuyenMai[0].khuyenMai.ngayKT;
            if(ISS.DATE.isBetWeen(_ngayBD, _ngayKT)) {
               cost = ISS.STRING_UTIL.format(TEMPLATE.Product.itemDetailCost,{
                  cost: _chiTietSanPhams[c].giaSP * _chiTietSanPhams[c].chiTietKhuyenMai[0].phanTram /100
               });
               image = ISS.STRING_UTIL.format(TEMPLATE.Product.itemDetailImage,{
                  image: ISS.SESSION.getSession('contextPath') + _chiTietSanPhams[c].hinhAnhSanPham[0].tenHinh,
                  off: _chiTietSanPhams[c].chiTietKhuyenMai[0].phanTram
               });
               break;
            }
            else {
               if(c == _chiTietSanPhams.length - 1) {
                  cost = ISS.STRING_UTIL.format(TEMPLATE.Product.itemDetailCost,{
                     cost: _chiTietSanPhams[0].giaSP
                  });
                  if(_chiTietSanPhams[0].hinhAnhSanPham.length > 0) {
                     image = ISS.STRING_UTIL.format(TEMPLATE.Product.itemDetailImage,{
                        image: ISS.SESSION.getSession('contextPath') + _chiTietSanPhams[0].hinhAnhSanPham[0].tenHinh,
                        off: '0'
                     });
                  }
                  else {
                     image = ISS.STRING_UTIL.format(TEMPLATE.Product.itemDetailImage,{
                        image: ISS.SESSION.getSession('contextPath'),
                        off: '0'
                     });
                  }
               }
            }
         }
         else {
            if(c == _chiTietSanPhams.length - 1) {
               cost = ISS.STRING_UTIL.format(TEMPLATE.Product.itemDetailCost,{
                  cost: _chiTietSanPhams[0].giaSP
               });
               if(_chiTietSanPhams[0].hinhAnhSanPham.length > 0) {
                  image = ISS.STRING_UTIL.format(TEMPLATE.Product.itemDetailImage,{
                     image: ISS.SESSION.getSession('contextPath') + _chiTietSanPhams[0].hinhAnhSanPham[0].tenHinh,
                     off: '0'
                  });
               }
               else {
                  image = ISS.STRING_UTIL.format(TEMPLATE.Product.itemDetailImage,{
                     image: ISS.SESSION.getSession('contextPath'),
                     off: '0'
                  });
               }
            }
         }
      }
      product += ISS.STRING_UTIL.format(TEMPLATE.Product.item,{
         itemDetailSize: size,
         image: image,
         tenSP: lstProduct[i].tenSP,
         thongTin: lstProduct[i].thongTin,
         cost: cost,
         detailPage: ISS.SESSION.getSession('contextPath') + '/service/product/toPageProductDetail/' + lstProduct[i].maSP
      });
   }
   ISS.DOM.html('#productsCatalog', product);
   ISS.DOM.paging('#recommentClothes');
}

function getProductDetailComplete(product) {
   mapProductDetailToView(product);
   mapProductToTab(product);
   renderHeaderLink(product);
}

function mapProductToTab(product) {
   var _size = '', _color = '';
   if(product.size){
      for(var i = 0; i < product.size.length; i++) {
         _size += product.size[i].tenSize + ', ';
      }
   }

   if(product.mauSac){
      for(i = 0; i < product.mauSac.length; i++) {
         _color += product.mauSac[i].tenMau + ', ';
      }
   }

   ISS.DOM.html('#tab_name', product.tenSP);
   ISS.DOM.html('#tab_info', product.thongTin);
   ISS.DOM.html('#tab_size', _size);
   ISS.DOM.html('#tab_color', _color);
}

function renderHeaderLink(product){
   var gender = ISS.SESSION.getSession('session_gender');
   var _html;
   if(gender == 'NAM') {
      _html = ISS.STRING_UTIL.format(TEMPLATE.ProductDetail.bread ,{
         shopPage: ISS.SESSION.getSession('contextPath'),
         genderPage: ISS.SESSION.getSession('contextPath') + '/jsp/user/user-men.jsp',
         gender: 'Nam',
         productPage: ISS.SESSION.getSession('contextPath') + '/service/product/toPageProduct/'+ ISS.SESSION.getSession('session_gender') + '/' + _product.loaiDanhMucSanPham.maLoaiDMSP,
         tenDMSP: _product.loaiDanhMucSanPham.tenLoaiDMSP
      })
   }
   else {
      _html = ISS.STRING_UTIL.format(TEMPLATE.ProductDetail.bread ,{
         shopPage: ISS.SESSION.getSession('contextPath'),
         genderPage: ISS.SESSION.getSession('contextPath') + '/jsp/user/user-women.jsp',
         gender: 'Nữ',
         productPage: ISS.SESSION.getSession('contextPath') + '/service/product/toPageProduct/'+ ISS.SESSION.getSession('session_gender') + '/' + _product.loaiDanhMucSanPham.maLoaiDMSP,
         tenDMSP: _product.loaiDanhMucSanPham.tenLoaiDMSP
      })
   }

   ISS.DOM.html('#product_detail_bread', _html);
}

function mapProductDetailToView(product){
   // info
   ISS.DOM.html('#product_detail_name',product.tenSP);
   ISS.DOM.html('#product_detail_nameDM',product.loaiDanhMucSanPham.tenLoaiDMSP);
   ISS.DOM.html('#product_detail_price_discount','');
   ISS.DOM.html('#product_detail_price', ISS.STRING_UTIL.formatNumeral( product.chiTietSanPhams[0].giaSP, '0,0'));
   // image
   product.chiTietSanPhams[0].contextPath = ISS.SESSION.getSession('contextPath');
   var _lstImage = ISS.TEMPLATE.handleBar.renderByString(TEMPLATE.ProductDetail.image ,product.chiTietSanPhams[0]);
   delete product.chiTietSanPhams[0].contextPath;
   ISS.DOM.html('#product_detail_list_images',_lstImage);
   if(product.chiTietSanPhams[0].hinhAnhSanPham.length > 0) {
      ISS.DOM.setAtt('#product_detail_big_image','src',ISS.SESSION.getSession('contextPath') + product.chiTietSanPhams[0].hinhAnhSanPham[0].tenHinh);
   }
   else {
      ISS.DOM.setAtt('#product_detail_big_image','src','../../..');
   }

   // size and color
   var _sizeHTML = ISS.TEMPLATE.handleBar.renderByString(TEMPLATE.ProductDetail.size ,product);
   var _colorHTML = ISS.TEMPLATE.handleBar.renderByString(TEMPLATE.ProductDetail.color ,product);
   ISS.DOM.html('#product_detail_list_size_color',_colorHTML);
   ISS.DOM.after('#product_detail_color', _sizeHTML);
   _productDetailImage = product.chiTietSanPhams[0];
   bindEventDetail();
   bindEventDetailImage();
}

function  bindEventDetail(){
   ISS.EVENT.bindEvent('#product_detail_size', 'change', function () {
      getProductDetailSeclect();
   });

   ISS.EVENT.bindEvent('#product_detail_color', 'change', function () {
      getProductDetailSeclect();
   });

   ISS.EVENT.bindEvent('#AddToCart', 'click', function () {
      if(_productDetailSelect) {
         ISS.BACKEND.request({
            url: ISS.SESSION.getSession('contextPath') + '/service/product/addCart',
            headers: {
               'Accept': 'application/json',
               'Content-Type': 'application/json'
            },
            type: 'POST',
            dataType: 'json',
            data: JSON.stringify(_productDetailSelect),
            success: function (data) {
               if(data.code == 0) {
                  var responseContent = data.responseObject;
                  mapCartToView(responseContent);
               }
               else {
                  alert('Không đủ số lượng !');
               }
            }
         });
      }
      else {
         alert('Bạn hãy chọn màu sắc và size');
      }
   });

   // event tab info
   for(var i = 1; i < 4; i++) {
      ISS.EVENT.bindEvent('#productDetails_tab_' + i, 'click', function () {
         var _index = ISS.DOM.getAtt('#' + this.id, 'index');

         resetTab();

         productDetailShowTab(this.id, _index);
      });
   }
}

function productDetailShowTab(id, index) {
   ISS.DOM.addClass('#' + id, 'active')
   ISS.DOM.addClass('#productDetails_tab_content_' + index, 'active');
}

function resetTab() {
   for(var i = 1; i < 4; i++) {
      ISS.DOM.removeClass('#productDetails_tab_' + i, 'active');
      ISS.DOM.removeClass('#productDetails_tab_content_' + i, 'active');
   }
}

function getProductDetailSeclect() {
   var _size = ISS.DOM.getVal('#product_detail_size');
   var _color = ISS.DOM.getVal('#product_detail_color');

   // product has size and color
   if(_product.size.length > 0 && _product.mauSac.length > 0) {
      if(!_size || !_color) {
         _productDetailSelect = null;
         ISS.DOM.html('#product-option-stock-number','Hãy chọn màu sắc và size');
      }
      else {
         var _lstProductDetail = _product.chiTietSanPhams;
         for(var i = 0; _lstProductDetail.length; i++) {
            if(_size == _lstProductDetail[i].kichThuoc.maSize && _color == _lstProductDetail[i].mauSac.maMau) {
               _productDetailSelect = _lstProductDetail[i];
               _productDetailImage = _lstProductDetail[i];
               ISS.DOM.html('#product-option-stock-number','Số lượng còn :' + _lstProductDetail[i].soLuong);

               // render image product detail selected
               _lstProductDetail[i].contextPath = ISS.SESSION.getSession('contextPath');
               var _lstImage = ISS.TEMPLATE.handleBar.renderByString(TEMPLATE.ProductDetail.image ,_lstProductDetail[i]);
               delete _lstProductDetail[i].contextPath;
               ISS.DOM.html('#product_detail_list_images',_lstImage);
               if(_lstProductDetail[i].hinhAnhSanPham[0]){
                  ISS.DOM.setAtt('#product_detail_big_image','src',ISS.SESSION.getSession('contextPath') + _lstProductDetail[i].hinhAnhSanPham[0].tenHinh);
               }
               else {
                  ISS.DOM.setAtt('#product_detail_big_image','src','../../..');
               }

               // render price and price discount
               renderPrice(_lstProductDetail[i]);
               // bind event image
               bindEventDetailImage(_lstProductDetail[i]);
               break;
            }
         }
      }
   }
   // product only has size
   else if(_product.size.length > 0 && _product.mauSac.length == 0){
      if(!_size) {
         _productDetailSelect = null;
         ISS.DOM.html('#product-option-stock-number','Hãy chọn màu sắc và size');
      }
      else {
         var _lstProductDetail = _product.chiTietSanPhams;
         for(var i = 0; _lstProductDetail.length; i++) {
            if(_size == _lstProductDetail[i].kichThuoc.maSize ) {
               _productDetailSelect = _lstProductDetail[i];
               _productDetailImage = _lstProductDetail[i];
               ISS.DOM.html('#product-option-stock-number','Số lượng còn :' + _lstProductDetail[i].soLuong);

               // render image product detail selected
               _lstProductDetail[i].contextPath = ISS.SESSION.getSession('contextPath');
               var _lstImage = ISS.TEMPLATE.handleBar.renderByString(TEMPLATE.ProductDetail.image ,_lstProductDetail[i]);
               delete _lstProductDetail[i].contextPath;
               ISS.DOM.html('#product_detail_list_images',_lstImage);
               if(_lstProductDetail[i].hinhAnhSanPham[0]){
                  ISS.DOM.setAtt('#product_detail_big_image','src',ISS.SESSION.getSession('contextPath') + _lstProductDetail[i].hinhAnhSanPham[0].tenHinh);
               }
               else {
                  ISS.DOM.setAtt('#product_detail_big_image','src','../../..');
               }

               // render price and price discount
               renderPrice(_lstProductDetail[i]);
               // bind event image
               bindEventDetailImage(_lstProductDetail[i]);
               break;
            }
         }
      }
   }
   // product only has color
   else if(_product.size.length == 0 && _product.mauSac.length > 0){
      if(!_color) {
         _productDetailSelect = null;
         ISS.DOM.html('#product-option-stock-number','Hãy chọn màu sắc và size');
      }
      else {
         var _lstProductDetail = _product.chiTietSanPhams;
         for(var i = 0; _lstProductDetail.length; i++) {
            if(_color == _lstProductDetail[i].mauSac.maMau ) {
               _productDetailSelect = _lstProductDetail[i];
               _productDetailImage = _lstProductDetail[i];
               ISS.DOM.html('#product-option-stock-number','Số lượng còn :' + _lstProductDetail[i].soLuong);

               // render image product detail selected
               _lstProductDetail[i].contextPath = ISS.SESSION.getSession('contextPath');
               var _lstImage = ISS.TEMPLATE.handleBar.renderByString(TEMPLATE.ProductDetail.image ,_lstProductDetail[i]);
               delete _lstProductDetail[i].contextPath;
               ISS.DOM.html('#product_detail_list_images',_lstImage);
               if(_lstProductDetail[i].hinhAnhSanPham[0]){
                  ISS.DOM.setAtt('#product_detail_big_image','src',ISS.SESSION.getSession('contextPath') + _lstProductDetail[i].hinhAnhSanPham[0].tenHinh);
               }
               else {
                  ISS.DOM.setAtt('#product_detail_big_image','src','../../..');
               }

               // render price and price discount
               renderPrice(_lstProductDetail[i]);
               // bind event image
               bindEventDetailImage(_lstProductDetail[i]);
               break;
            }
         }
      }
   }
   // product don't have color and size
   else {
      _productDetailSelect = _lstProductDetail[0];
      _productDetailImage = _lstProductDetail[0];
   }
}

function bindEventDetailImage(productDetail) {
   var lstImage = _productDetailImage.hinhAnhSanPham;
   for(var i = 0; i < lstImage.length; i++) {
      ISS.EVENT.bindEvent('#product_detail_color_image_' + lstImage[i].maHinhSP, 'click', function () {
         var image = ISS.DOM.getAtt('#' + this.id, 'src');
         ISS.DOM.setAtt('#product_detail_big_image', 'src', image);
      });
   }
}

function renderPrice(productDetail){
   ISS.DOM.html('#product_detail_price', ISS.STRING_UTIL.formatNumeral( productDetail.giaSP, '0,0'));

   var _chiTietKhuyenMai = productDetail.chiTietKhuyenMai;
   if(_chiTietKhuyenMai.length > 0) {
      for(var i = 0; i < _chiTietKhuyenMai.length; i++){
         if(ISS.DATE.isBetWeen(_chiTietKhuyenMai[i].khuyenMai.ngayBD, _chiTietKhuyenMai[i].khuyenMai.ngayKT)) {
            ISS.DOM.html('#product_detail_price_discount', _chiTietKhuyenMai[i].phanTram);
            ISS.DOM.css('#product_detail_price_discount_display',{"display":"block"});
         }
         else {
            ISS.DOM.html('#product_detail_price_discount','');
            ISS.DOM.css('#product_detail_price_discount_display',{"display":"none"});
         }
      }
   }
   else {
      ISS.DOM.html('#product_detail_price_discount','');
      ISS.DOM.css('#product_detail_price_discount_display',{"display":"none"});
   }
}

